<template>
  <div>
    <el-form label-width="80px">
      <el-form-item label="SKU"> {{ spuName }} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          type="number"
          v-model="skuInfo.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          placeholder="规格描述"
          rows="4"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
          >
            <el-select v-model="attr.attrIdAndattrValueId" placeholder="请选择">
              <el-option
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
                v-for="attrValue in attr.attrValueList"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="saleAttr in spuSaleAttrList"
            :key="saleAttr.id"
          >
            <el-select
              v-model="saleAttr.saleAttrIdAndsaleAttrValueId"
              placeholder="请选择"
            >
              <el-option
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
                v-for="saleAttrValue in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
          border
          :data="spuImageList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="80"> </el-table-column>
          <el-table-column prop="prop" label="图片展示" width="width">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" alt="" style="width: 100px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="图片名称" width="width">
          </el-table-column
          ><el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <el-tag v-if="row.isDefault == 0" @click="changeIsDefault(row)"
                >设置默认</el-tag
              >
              <el-tag v-else type="success">默认</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "AddSku",
  data() {
    return {
      // 存储图片的信息
      spuImageList: [],
      // 存储销售属性的信息
      spuSaleAttrList: [],
      // 存储平台属性的信息
      attrInfoList: [],
      //收集sku相关的的字段
      skuInfo: {
        // 第一类数据：父组件给的数据
        spuId: 0,
        category3Id: 0,
        tmId: 0,
        // 第二类数据:需要通过v-model双向绑定收集
        price: 0,
        skuName: "",
        weight: "",
        skuDesc: "",
        // 第三类：需要自己写的
        // 默认图片
        skuDefaultImg: "",
        // 平台属性
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0,
          // },
        ],
        // 收集图片的字段
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   skuId: 0,
          //   spuImgId: 0,
          // },
        ],
        // 销售属性
        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: "string",
          //   saleAttrValueId: 0,
          //   saleAttrValueName: "string",
          //   skuId: 0,
          //   spuId: 0,
          // },
        ],
      },
      // 收集图片数据的字段
      imageList: [],
      spuName: "",
    };
  },
  methods: {
    // 获取SkuForm数据
    async getData(category1Id, category2Id, spu) {
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.spuName = spu.spuName;
      console.log(spu);
      // 获取图片的数据
      const getSpuImageList = await this.$API.spu.reqSpuImageList(spu.id);
      // 获取销售属性的数据
      const getSpuSaleAttrList = await this.$API.spu.reqSpuSaleAttrList(spu.id);
      // 获取平台属性的数据
      const getAttrInfoList = await this.$API.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        spu.category3Id
      );
      //  Promise.all 并行处理接口
      Promise.all([getSpuImageList, getSpuSaleAttrList, getAttrInfoList]).then(
        (res) => {
          let list = res[0].data;
          // 整理图片数据
          list.forEach((item) => {
            item.isDefault = 0;
          });
          this.spuImageList = list;
          this.spuSaleAttrList = res[1].data;
          this.attrInfoList = res[2].data;
        }
      );
    },
    // table表格复选框按钮的事件  selection：当前选择的元素
    handleSelectionChange(selection) {
      this.imageList = selection;
    },
    // 切换默认
    changeIsDefault(row) {
      // 排他思想：遍历把全部的isDefault为0
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      // 当前点击的isDefault为1
      row.isDefault = 1;
      // 收集默认图片的地址
      this.skuDefaultImg = row.imgUrl;
    },
    // 点击取消按钮
    cancel() {
      // 自定义事件：通知父组件切换场景为0
      this.$emit("changeScene", 0);
      // 清除数据
      Object.assign(this._data, this.$options.data());
    },
    // 保存
    async save() {
      const { attrInfoList, skuInfo, spuSaleAttrList, spuImageList } = this;
      // 整理平台属性的参数
      attrInfoList.forEach((item) => {
        if (item.attrIdAndattrValueId) {
          let [attrId, valueId] = item.attrIdAndattrValueId.split(":");
          skuInfo.skuAttrValueList.push({ attrId, valueId });
        }
      });
      // 整理销售属性的参数
      spuSaleAttrList.forEach((item) => {
        if (item.saleAttrIdAndsaleAttrValueId) {
          let [saleAttrId, saleAttrValueId] =
            item.saleAttrIdAndsaleAttrValueId.split(":");
          skuInfo.skuSaleAttrValueList.push({ saleAttrId, saleAttrValueId });
        }
      });
      // 整理图片数据的参数
      skuInfo.skuImageList = spuImageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });

      // 发请求
      let result = await this.$API.spu.reqAddSku(skuInfo);
      if (result.code == 200) {
        // 返回场景0
        this.$emit("changeScene", 0);
        this.$message.success("添加成功");
        // 清除数据
        Object.assign(this._data, this.$options.data());
      }
    },
  },
};
</script>

<style>
</style>